var alarms = [];
var numPie;
var timePie;
var start = $('#startTime').val();
var end = $('#endTime').val();
var stationId = $('#stationId').val();
function initPie() {
	$('#start').val($('#startTime').val());
	$('#end').val($('#endTime').val());
	$('#stationId').val($('#stationId').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});
	loadData();
}
function loadData() {
	stationId = $('#stationId').val();
	if (stationId) {
		$.getJSON("/rest/protectAlarm/getProtectInterruptAlarmByStationId/" + start + "/" + end + "/" + stationId,
				function(result) {
					alarms = result;
					loadPie(alarms);
				});
	} else {
		$.getJSON("/rest/protectAlarm/getProtectInterruptAlarm/" + start + "/" + end, function(result) {
			alarms = result;
			loadPie(alarms);
		});
	}
}
function pieQuery() {
	var s = $('#start').val();
	var e = $('#end').val();
	start = s;
	end = e;
	loadData();
}
function loadPie(alarms) {
	var counts = {
		interrupt : 0,
		other : 0
	};
	var durationTimes = {
		interrupt : 0,
		other : 0
	};
	for ( var i in alarms) {
		var r = alarms[i];
		if (r.channelAlarms && r.channelAlarms.length > 0) {
			counts.interrupt++;
			durationTimes.interrupt += (r.durationTime ? r.durationTime : 0);
		} else {
			counts.other++;
			durationTimes.other += (r.durationTime ? r.durationTime : 0);
		}
	}
	$('#count').html(alarms.length);
	$('#count2').html(alarms.length);
	$('#interruptCount').html(counts.interrupt);
	var time = (durationTimes.other + durationTimes.interrupt) / 1000 / 60 / 60;
	time = time.toFixed(2);
	var interruptTime = durationTimes.interrupt / 1000 / 60 / 60;
	interruptTime = interruptTime.toFixed(2);
	$('#time').html(time);
	$('#time2').html(time);
	$('#interruptTime').html(interruptTime);
	var color1 = '#dd4b39';
	var color2 = 'cadetblue';
	numPie = echarts.init(document.getElementById("numPie"));
	var numPieData = [];
	numPieData.push({
		name : locale.TongXinTongDaoZhongDuan,
		value : counts.interrupt,
		itemStyle : {
			normal : {
				color : color1
			}
		}
	});
	if (counts.other > 0) {
		numPieData.push({
			name : locale.BaoHuTongDaoZhongDuan,
			value : counts.other,
			label : {
				normal : {
					show : false
				},
				emphasis : {
					show : false
				}
			},
			labelLine : {
				normal : {
					show : false
				},
				emphasis : {
					show : false
				}
			},
			itemStyle : {
				normal : {
					color : color2
				}
			}
		});
	}
	var numOption = {
		series : [ {
			name : locale.BaoHuTongXinZhongDuanCiShu,
			type : 'pie',
			label : {
				normal : {
					fontSize : 18,
					formatter : '{b}\n{c}'+locale.Ci
				}
			},
			data : numPieData
		} ]
	};
	numPie.setOption(numOption);

	timePie = echarts.init(document.getElementById("timePie"));
	var timePieData = [];
	timePieData.push({
		name :locale.TongXinTongDaoZhongDuan ,
		value : interruptTime,
		itemStyle : {
			normal : {
				color : color1
			}
		}
	});
	if (time - interruptTime > 0) {
		timePieData.push({
			name : locale.BaoHuTongDaoZhongDuan,
			value : time - interruptTime,
			label : {
				normal : {
					show : false
				},
				emphasis : {
					show : false
				}
			},
			labelLine : {
				normal : {
					show : false
				},
				emphasis : {
					show : false
				}
			},
			itemStyle : {
				normal : {
					color : color2
				}
			}
		});
	}
	var timeOption = {
		series : [ {
			name : locale.BaoHuTongXinZhongDuanShiJian,
			type : 'pie',
			label : {
				normal : {
					fontSize : 18,
					formatter : '{b}\n{c}'+locale.XiaoShi
				}
			},
			data : timePieData
		} ]
	};
	timePie.setOption(timeOption);
}
function resizeHtml() {
	if (numPie) {
		numPie.resize();
	}
	if (timePie) {
		timePie.resize();
	}
}
